﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace chuankou
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SerialPort comm = new SerialPort();//新增串口对象
        StringBuilder builder = new StringBuilder();//新增缓存变量
        //string buffer_rev = "";//接收串口的缓存
        //ConfigurationManager ConMg = new ConfigurationManager;
        private void ReadCongigToUI()
        {
            numericUpDown1.Value = int.Parse(ConfigurationManager.AppSettings["角速度_ROLL_P"]);//从配置文件中读取数据
            numericUpDown2.Value = int.Parse(ConfigurationManager.AppSettings["角速度_ROLL_I"]);
            numericUpDown3.Value = int.Parse(ConfigurationManager.AppSettings["角速度_ROLL_D"]);
            numericUpDown4.Value = int.Parse(ConfigurationManager.AppSettings["角速度_PITCH_P"]);
            numericUpDown5.Value = int.Parse(ConfigurationManager.AppSettings["角速度_PITCH_I"]);
            numericUpDown6.Value = int.Parse(ConfigurationManager.AppSettings["角速度_PITCH_D"]);
            numericUpDown7.Value = int.Parse(ConfigurationManager.AppSettings["角速度_YAW_P"]);
            numericUpDown8.Value = int.Parse(ConfigurationManager.AppSettings["角速度_YAW_I"]);
            numericUpDown9.Value = int.Parse(ConfigurationManager.AppSettings["角速度_YAW_D"]);

            numericUpDown18.Value = int.Parse(ConfigurationManager.AppSettings["角度_ROLL_P"]);//从配置文件中读取数据
            numericUpDown17.Value = int.Parse(ConfigurationManager.AppSettings["角度_ROLL_I"]);
            numericUpDown16.Value = int.Parse(ConfigurationManager.AppSettings["角度_ROLL_D"]);
            numericUpDown15.Value = int.Parse(ConfigurationManager.AppSettings["角度_PITCH_P"]);
            numericUpDown14.Value = int.Parse(ConfigurationManager.AppSettings["角度_PITCH_I"]);
            numericUpDown13.Value = int.Parse(ConfigurationManager.AppSettings["角度_PITCH_D"]);
            numericUpDown12.Value = int.Parse(ConfigurationManager.AppSettings["角度_YAW_P"]);
            numericUpDown11.Value = int.Parse(ConfigurationManager.AppSettings["角度_YAW_I"]);
            numericUpDown10.Value = int.Parse(ConfigurationManager.AppSettings["角度_YAW_D"]);

            numericUpDown27.Value = int.Parse(ConfigurationManager.AppSettings["速度_X_P"]);//从配置文件中读取数据
            numericUpDown26.Value = int.Parse(ConfigurationManager.AppSettings["速度_X_I"]);
            numericUpDown25.Value = int.Parse(ConfigurationManager.AppSettings["速度_X_D"]);
            numericUpDown24.Value = int.Parse(ConfigurationManager.AppSettings["速度_Y_P"]);
            numericUpDown23.Value = int.Parse(ConfigurationManager.AppSettings["速度_Y_I"]);
            numericUpDown22.Value = int.Parse(ConfigurationManager.AppSettings["速度_Y_D"]);
            numericUpDown21.Value = int.Parse(ConfigurationManager.AppSettings["速度_Z_P"]);
            numericUpDown20.Value = int.Parse(ConfigurationManager.AppSettings["速度_Z_I"]);
            numericUpDown19.Value = int.Parse(ConfigurationManager.AppSettings["速度_Z_D"]);

            numericUpDown36.Value = int.Parse(ConfigurationManager.AppSettings["位置_X_P"]);//从配置文件中读取数据
            numericUpDown35.Value = int.Parse(ConfigurationManager.AppSettings["位置_X_I"]);
            numericUpDown34.Value = int.Parse(ConfigurationManager.AppSettings["位置_X_D"]);
            numericUpDown33.Value = int.Parse(ConfigurationManager.AppSettings["位置_Y_P"]);
            numericUpDown32.Value = int.Parse(ConfigurationManager.AppSettings["位置_Y_I"]);
            numericUpDown31.Value = int.Parse(ConfigurationManager.AppSettings["位置_Y_D"]);
            numericUpDown30.Value = int.Parse(ConfigurationManager.AppSettings["位置_Z_P"]);
            numericUpDown29.Value = int.Parse(ConfigurationManager.AppSettings["位置_Z_I"]);
            numericUpDown28.Value = int.Parse(ConfigurationManager.AppSettings["位置_Z_D"]);

            numericUpDown37.Value = int.Parse(ConfigurationManager.AppSettings["陀螺仪机械校准_ROLL"]);//从配置文件中读取数据
            numericUpDown38.Value = int.Parse(ConfigurationManager.AppSettings["陀螺仪机械校准_PITCH"]);
        }
        private void button1_Click(object sender, EventArgs e)//OK----------------------------------------
        {
            comboBox1.Items.Clear();//移除所有项
            comboBox1.Items.AddRange(SerialPort.GetPortNames());//添加已有串口      
        }

        private void button2_Click(object sender, EventArgs e)//OK-----------------------------------------
        {
            comboBox1.Enabled = false;
            comboBox2.Enabled = false;
            //comm.NewLine = "\r\n";
            comm.RtsEnable = true;
            comm.ReadTimeout = 10000;//读取超时设置单位ms

            try
            {
                comm.PortName = (string)comboBox1.SelectedItem;


            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                comboBox1.Enabled = true;
                comboBox2.Enabled = true;
            }

            comm.DataBits = 8;//数据位
            try
            {
                comm.BaudRate = int.Parse((string)(comboBox2.SelectedItem));//波特率
                comm.Open();//打开串口
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                comboBox1.Enabled = true;
                comboBox2.Enabled = true;
            }
            button4.Enabled = true;
            button2.Enabled = false;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReadCongigToUI();
            button4.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)//OK断开串口按钮-----------------------------------------
        {
            comm.Close();
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
            button2.Enabled = true;
            button4.Enabled = false;
        }


        private void button3_Click(object sender, EventArgs e)//将所有参数存入外部配置文件
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings["角速度_ROLL_P"].Value = string.Concat(numericUpDown1.Value);
            config.AppSettings.Settings["角速度_ROLL_I"].Value = string.Concat(numericUpDown2.Value);
            config.AppSettings.Settings["角速度_ROLL_D"].Value = string.Concat(numericUpDown3.Value);
            config.AppSettings.Settings["角速度_PITCH_P"].Value = string.Concat(numericUpDown4.Value);
            config.AppSettings.Settings["角速度_PITCH_I"].Value = string.Concat(numericUpDown5.Value);
            config.AppSettings.Settings["角速度_PITCH_D"].Value = string.Concat(numericUpDown6.Value);
            config.AppSettings.Settings["角速度_YAW_P"].Value = string.Concat(numericUpDown7.Value);
            config.AppSettings.Settings["角速度_YAW_I"].Value = string.Concat(numericUpDown8.Value);
            config.AppSettings.Settings["角速度_YAW_D"].Value = string.Concat(numericUpDown9.Value);

            config.AppSettings.Settings["角度_ROLL_P"].Value = string.Concat(numericUpDown18.Value);
            config.AppSettings.Settings["角度_ROLL_I"].Value = string.Concat(numericUpDown17.Value);
            config.AppSettings.Settings["角度_ROLL_D"].Value = string.Concat(numericUpDown16.Value);
            config.AppSettings.Settings["角度_PITCH_P"].Value = string.Concat(numericUpDown15.Value);
            config.AppSettings.Settings["角度_PITCH_I"].Value = string.Concat(numericUpDown14.Value);
            config.AppSettings.Settings["角度_PITCH_D"].Value = string.Concat(numericUpDown13.Value);
            config.AppSettings.Settings["角度_YAW_P"].Value = string.Concat(numericUpDown12.Value);
            config.AppSettings.Settings["角度_YAW_I"].Value = string.Concat(numericUpDown11.Value);
            config.AppSettings.Settings["角度_YAW_D"].Value = string.Concat(numericUpDown10.Value);

            config.AppSettings.Settings["速度_X_P"].Value = string.Concat(numericUpDown27.Value);
            config.AppSettings.Settings["速度_X_I"].Value = string.Concat(numericUpDown26.Value);
            config.AppSettings.Settings["速度_X_D"].Value = string.Concat(numericUpDown25.Value);
            config.AppSettings.Settings["速度_Y_P"].Value = string.Concat(numericUpDown24.Value);
            config.AppSettings.Settings["速度_Y_I"].Value = string.Concat(numericUpDown23.Value);
            config.AppSettings.Settings["速度_Y_D"].Value = string.Concat(numericUpDown22.Value);
            config.AppSettings.Settings["速度_Z_P"].Value = string.Concat(numericUpDown21.Value);
            config.AppSettings.Settings["速度_Z_I"].Value = string.Concat(numericUpDown20.Value);
            config.AppSettings.Settings["速度_Z_D"].Value = string.Concat(numericUpDown19.Value);

            config.AppSettings.Settings["位置_X_P"].Value = string.Concat(numericUpDown36.Value);
            config.AppSettings.Settings["位置_X_I"].Value = string.Concat(numericUpDown35.Value);
            config.AppSettings.Settings["位置_X_D"].Value = string.Concat(numericUpDown34.Value);
            config.AppSettings.Settings["位置_Y_P"].Value = string.Concat(numericUpDown33.Value);
            config.AppSettings.Settings["位置_Y_I"].Value = string.Concat(numericUpDown32.Value);
            config.AppSettings.Settings["位置_Y_D"].Value = string.Concat(numericUpDown31.Value);
            config.AppSettings.Settings["位置_Z_P"].Value = string.Concat(numericUpDown30.Value);
            config.AppSettings.Settings["位置_Z_I"].Value = string.Concat(numericUpDown29.Value);
            config.AppSettings.Settings["位置_Z_D"].Value = string.Concat(numericUpDown28.Value);

            config.AppSettings.Settings["陀螺仪机械校准_ROLL"].Value = string.Concat(numericUpDown37.Value);
            config.AppSettings.Settings["陀螺仪机械校准_PITCH"].Value = string.Concat(numericUpDown38.Value);

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
        }

        private void button5_Click(object sender, EventArgs e)//从配置文件中读取参数到UI中-------------------------------------------------------
        {
            ReadCongigToUI();
        }
        //---------------------------------------------------------------------------------------------------------------------------------------------------------
        private void button7_Click(object sender, EventArgs e)//OK-----------------------------------------
        {
            int i;
            Byte[] TxData = new byte[21];

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            TxData[0] = 1;//功能字
            TxData[1] = 18;//数据长度

            TxData[2] = (byte)(numericUpDown1.Value / 256);
            TxData[3] = (byte)(numericUpDown1.Value % 256);//角速度_ROLL_P  高位在前低位在后后面同理

            TxData[4] = (byte)(numericUpDown2.Value / 256);
            TxData[5] = (byte)(numericUpDown2.Value % 256);

            TxData[6] = (byte)(numericUpDown3.Value / 256);
            TxData[7] = (byte)(numericUpDown3.Value % 256);

            TxData[8] = (byte)(numericUpDown4.Value / 256);
            TxData[9] = (byte)(numericUpDown4.Value % 256);

            TxData[10] = (byte)(numericUpDown5.Value / 256);
            TxData[11] = (byte)(numericUpDown5.Value % 256);

            TxData[12] = (byte)(numericUpDown6.Value / 256);
            TxData[13] = (byte)(numericUpDown6.Value % 256);

            TxData[14] = (byte)(numericUpDown7.Value / 256);
            TxData[15] = (byte)(numericUpDown7.Value % 256);

            TxData[16] = (byte)(numericUpDown8.Value / 256);
            TxData[17] = (byte)(numericUpDown8.Value % 256);

            TxData[18] = (byte)(numericUpDown9.Value / 256);
            TxData[19] = (byte)(numericUpDown9.Value % 256);

            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try//发送数据
            {
                comm.DiscardOutBuffer();
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            //接受返回校验
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                if (RxData[2] == 0XFF)
                {
                    label27.Text = "写入成功";
                    label27.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    label27.Text = "写入失败";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "返回失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }
        private void button6_Click(object sender, EventArgs e)//请求角度PID
        {
            int i;

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            Byte[] TxData = new byte[21];
            TxData[0] = 2;//功能字
            TxData[1] = 0;//数据长度
            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            //接收返回的PID参数
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                byte sum = 0;
                for (i = 0; i < 20; i++)
                {
                    sum += RxData[i];
                }
                if (sum == RxData[20])
                {
                    label27.Text = "数据正确";
                    label27.ForeColor = System.Drawing.Color.Green;

                    numericUpDown1.Value = RxData[2] * 256 + RxData[3];
                    numericUpDown2.Value = RxData[4] * 256 + RxData[5];
                    numericUpDown3.Value = RxData[6] * 256 + RxData[7];
                    numericUpDown4.Value = RxData[8] * 256 + RxData[9];
                    numericUpDown5.Value = RxData[10] * 256 + RxData[11];
                    numericUpDown6.Value = RxData[12] * 256 + RxData[13];
                    numericUpDown7.Value = RxData[14] * 256 + RxData[15];
                    numericUpDown8.Value = RxData[16] * 256 + RxData[17];
                    numericUpDown9.Value = RxData[18] * 256 + RxData[19];
                }
                else
                {
                    label27.Text = "数据错误";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }
        private void button8_Click(object sender, EventArgs e)//角度pid写入
        {
            int i;
            Byte[] TxData = new byte[21];

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            TxData[0] = 3;//功能字
            TxData[1] = 18;//数据长度

            TxData[2] = (byte)(numericUpDown18.Value / 256);
            TxData[3] = (byte)(numericUpDown18.Value % 256);//角度_ROLL_P  高位在前低位在后后面同理

            TxData[4] = (byte)(numericUpDown17.Value / 256);
            TxData[5] = (byte)(numericUpDown17.Value % 256);

            TxData[6] = (byte)(numericUpDown16.Value / 256);
            TxData[7] = (byte)(numericUpDown16.Value % 256);

            TxData[8] = (byte)(numericUpDown15.Value / 256);
            TxData[9] = (byte)(numericUpDown15.Value % 256);

            TxData[10] = (byte)(numericUpDown14.Value / 256);
            TxData[11] = (byte)(numericUpDown14.Value % 256);

            TxData[12] = (byte)(numericUpDown13.Value / 256);
            TxData[13] = (byte)(numericUpDown13.Value % 256);

            TxData[14] = (byte)(numericUpDown12.Value / 256);
            TxData[15] = (byte)(numericUpDown12.Value % 256);

            TxData[16] = (byte)(numericUpDown11.Value / 256);
            TxData[17] = (byte)(numericUpDown11.Value % 256);

            TxData[18] = (byte)(numericUpDown10.Value / 256);
            TxData[19] = (byte)(numericUpDown10.Value % 256);

            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            //校验
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                if (RxData[2] == 0XFF)
                {
                    label27.Text = "写入成功";
                    label27.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    label27.Text = "写入失败";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int i;

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            Byte[] TxData = new byte[21];
            TxData[0] = 4;//功能字
            TxData[1] = 0;//数据长度
            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            //接收返回的PID
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                byte sum = 0;
                for (i = 0; i < 20; i++)
                {
                    sum += RxData[i];
                }
                if (sum == RxData[20])
                {
                    label27.Text = "数据正确";
                    label27.ForeColor = System.Drawing.Color.Green;

                    numericUpDown18.Value = RxData[2] * 256 + RxData[3];
                    numericUpDown17.Value = RxData[4] * 256 + RxData[5];
                    numericUpDown16.Value = RxData[6] * 256 + RxData[7];
                    numericUpDown15.Value = RxData[8] * 256 + RxData[9];
                    numericUpDown14.Value = RxData[10] * 256 + RxData[11];
                    numericUpDown13.Value = RxData[12] * 256 + RxData[13];
                    numericUpDown12.Value = RxData[14] * 256 + RxData[15];
                    numericUpDown11.Value = RxData[16] * 256 + RxData[17];
                    numericUpDown10.Value = RxData[18] * 256 + RxData[19];
                }
                else
                {
                    label27.Text = "数据错误";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            int i;
            Byte[] TxData = new byte[21];
            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            TxData[0] = 5;//功能字
            TxData[1] = 18;//数据长度

            TxData[2] = (byte)(numericUpDown27.Value / 256);
            TxData[3] = (byte)(numericUpDown27.Value % 256);//角度_ROLL_P  高位在前低位在后后面同理

            TxData[4] = (byte)(numericUpDown26.Value / 256);
            TxData[5] = (byte)(numericUpDown26.Value % 256);

            TxData[6] = (byte)(numericUpDown25.Value / 256);
            TxData[7] = (byte)(numericUpDown25.Value % 256);

            TxData[8] = (byte)(numericUpDown24.Value / 256);
            TxData[9] = (byte)(numericUpDown24.Value % 256);

            TxData[10] = (byte)(numericUpDown23.Value / 256);
            TxData[11] = (byte)(numericUpDown23.Value % 256);

            TxData[12] = (byte)(numericUpDown22.Value / 256);
            TxData[13] = (byte)(numericUpDown22.Value % 256);

            TxData[14] = (byte)(numericUpDown21.Value / 256);
            TxData[15] = (byte)(numericUpDown21.Value % 256);

            TxData[16] = (byte)(numericUpDown20.Value / 256);
            TxData[17] = (byte)(numericUpDown20.Value % 256);

            TxData[18] = (byte)(numericUpDown19.Value / 256);
            TxData[19] = (byte)(numericUpDown19.Value % 256);

            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            //校验

            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                if (RxData[2] == 0XFF)
                {
                    label27.Text = "写入成功";
                    label27.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    label27.Text = "写入失败";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int i;

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            Byte[] TxData = new byte[21];
            TxData[0] = 6;//功能字
            TxData[1] = 0;//数据长度
            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                byte sum = 0;
                for (i = 0; i < 20; i++)
                {
                    sum += RxData[i];
                }
                if (sum == RxData[20])
                {
                    label27.Text = "数据正确";
                    label27.ForeColor = System.Drawing.Color.Green;

                    numericUpDown27.Value = RxData[2] * 256 + RxData[3];
                    numericUpDown26.Value = RxData[4] * 256 + RxData[5];
                    numericUpDown25.Value = RxData[6] * 256 + RxData[7];
                    numericUpDown24.Value = RxData[8] * 256 + RxData[9];
                    numericUpDown23.Value = RxData[10] * 256 + RxData[11];
                    numericUpDown22.Value = RxData[12] * 256 + RxData[13];
                    numericUpDown21.Value = RxData[14] * 256 + RxData[15];
                    numericUpDown20.Value = RxData[16] * 256 + RxData[17];
                    numericUpDown19.Value = RxData[18] * 256 + RxData[19];
                }
                else
                {
                    label27.Text = "数据错误";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            int i;
            Byte[] TxData = new byte[21];

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            TxData[0] = 7;//功能字
            TxData[1] = 18;//数据长度

            TxData[2] = (byte)(numericUpDown36.Value / 256);
            TxData[3] = (byte)(numericUpDown36.Value % 256);//角度_ROLL_P  高位在前低位在后后面同理

            TxData[4] = (byte)(numericUpDown35.Value / 256);
            TxData[5] = (byte)(numericUpDown35.Value % 256);

            TxData[6] = (byte)(numericUpDown34.Value / 256);
            TxData[7] = (byte)(numericUpDown34.Value % 256);

            TxData[8] = (byte)(numericUpDown33.Value / 256);
            TxData[9] = (byte)(numericUpDown33.Value % 256);

            TxData[10] = (byte)(numericUpDown32.Value / 256);
            TxData[11] = (byte)(numericUpDown32.Value % 256);

            TxData[12] = (byte)(numericUpDown31.Value / 256);
            TxData[13] = (byte)(numericUpDown31.Value % 256);

            TxData[14] = (byte)(numericUpDown30.Value / 256);
            TxData[15] = (byte)(numericUpDown30.Value % 256);

            TxData[16] = (byte)(numericUpDown29.Value / 256);
            TxData[17] = (byte)(numericUpDown29.Value % 256);

            TxData[18] = (byte)(numericUpDown28.Value / 256);
            TxData[19] = (byte)(numericUpDown28.Value % 256);

            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            //校验
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                if (RxData[2] == 0XFF)
                {
                    label27.Text = "写入成功";
                    label27.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    label27.Text = "写入失败";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            int i;

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            Byte[] TxData = new byte[21];
            TxData[0] = 8;//功能字
            TxData[1] = 0;//数据长度
            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                byte sum = 0;
                for (i = 0; i < 20; i++)
                {
                    sum += RxData[i];
                }
                if (sum == RxData[20])
                {
                    label27.Text = "数据正确";
                    label27.ForeColor = System.Drawing.Color.Green;

                    numericUpDown36.Value = RxData[2] * 256 + RxData[3];
                    numericUpDown35.Value = RxData[4] * 256 + RxData[5];
                    numericUpDown34.Value = RxData[6] * 256 + RxData[7];
                    numericUpDown33.Value = RxData[8] * 256 + RxData[9];
                    numericUpDown32.Value = RxData[10] * 256 + RxData[11];
                    numericUpDown31.Value = RxData[12] * 256 + RxData[13];
                    numericUpDown30.Value = RxData[14] * 256 + RxData[15];
                    numericUpDown29.Value = RxData[16] * 256 + RxData[17];
                    numericUpDown28.Value = RxData[18] * 256 + RxData[19];
                }
                else
                {
                    label27.Text = "数据错误";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button17_Click(object sender, EventArgs e)//机械矫正数据为了便于传输传输之前自动加500
        {
            int i;
            Byte[] TxData = new byte[21];

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            TxData[0] = 9;//功能字
            TxData[1] = 4;//数据长度

            TxData[2] = (byte)((numericUpDown37.Value + 500) / 256);
            TxData[3] = (byte)((numericUpDown37.Value + 500) % 256);//角度_ROLL_P  高位在前低位在后后面同理

            TxData[4] = (byte)((numericUpDown38.Value + 500) / 256);
            TxData[5] = (byte)((numericUpDown38.Value + 500) % 256);

            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            //校验
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                if (RxData[2] == 0XFF)
                {
                    label27.Text = "写入成功";
                    label27.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    label27.Text = "写入失败";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void button18_Click(object sender, EventArgs e)//读取机械矫正数据
        {
            int i;

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            Byte[] TxData = new byte[21];
            TxData[0] = 10;//功能字
            TxData[1] = 0;//数据长度
            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();
                for (i = 0; i < 21; i++)
                {
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                byte sum = 0;
                for (i = 0; i < 20; i++)
                {
                    sum += RxData[i];
                }
                if (sum == RxData[20])
                {
                    label27.Text = "数据正确";
                    label27.ForeColor = System.Drawing.Color.Green;

                    numericUpDown37.Value = (RxData[2] * 256 + RxData[3]) - 500;
                    numericUpDown38.Value = (RxData[4] * 256 + RxData[5]) - 500;
                }
                else
                {
                    label27.Text = "数据错误";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }
        private void button16_Click(object sender, EventArgs e)
        {//把RAM中的PID缓存写入FLASH
            int i;

            label27.Text = "等待返回值";
            label27.ForeColor = System.Drawing.Color.Gray;

            Byte[] TxData = new byte[21];
            TxData[0] = 0xff;//功能字
            TxData[1] = 0;//数据长度
            TxData[20] = 0;
            for (i = 0; i < 20; i++)
            {
                TxData[20] += TxData[i];
            }
            try
            {
                comm.Write(TxData, 0, 21);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            //接收返回值
            Byte[] RxData = new byte[21];
            try
            {
                comm.DiscardInBuffer();//清除接收缓存
                for (i = 0; i < 21; i++)
                {//接收数据
                    RxData[i] = (byte)comm.ReadByte();
                }
                //comm.Write(RxData, 0, 21);
                if (RxData[2] == 0XFF)
                {
                    label27.Text = "刷入成功";
                    label27.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    label27.Text = "刷入失败";
                    label27.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
                label27.Text = "接收失败";
                label27.ForeColor = System.Drawing.Color.Red;
            }
        }
        //-------------------无边框窗体拖动---------------------------  
        Point mouseOff;//鼠标移动位置变量  
        bool leftFlag;//标签是否为左键  
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值  
                leftFlag = true;                  //点击左键按下时标注为true;  
            }
        }
        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置  
                (((System.Windows.Forms.PictureBox)sender).Parent).Location = mouseSet;
            }
        }
        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;  
            }
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        private void label28_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        //------------------------end 无边框窗体拖动-----------------------------------借鉴的代码
        private void button10_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
